package com.shangxia.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shangxia.feign.client.ProductClient;
import com.shangxia.feign.client.UserClient;
import com.shangxia.feign.model.SX_Order;
import com.shangxia.feign.model.SX_Order_Product;
import com.shangxia.order.mapper.OrderMapper;
import com.shangxia.order.mapper.OrderProductMapper;
import com.shangxia.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhouya38158
 * @Package com.shangxia.user.service.impl
 * @date 2021/11/2 14:46
 */
@Service("userService")
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderProductMapper orderProductMapper;
    @Autowired
    private UserClient userClient;
    @Autowired
    private ProductClient productClient;

    @Override
    public SX_Order queryById(Long id) {
        SX_Order order = orderMapper.selectById(id);
        order.setUser(userClient.queryById(order.getUserId()));
        QueryWrapper<SX_Order_Product> queryWrapper = new QueryWrapper();
        queryWrapper.eq("orderId", order.getId()).select("productId");
        List<SX_Order_Product> sx_order_products = orderProductMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(sx_order_products)) {
            order.setProductList(productClient.listByIds(sx_order_products.stream().map(SX_Order_Product::getProductId).collect(Collectors.toList())));
        }
        return order;
    }

    @Override
    public List<SX_Order> listSxOrder() {
        return orderMapper.selectList(new QueryWrapper());
    }
}
